#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define err_quit(msg) \
  do { \
    fprintf(stderr, msg": %s\n", strerror(errno));  \
    exit(EXIT_FAILURE);                             \
  } while (0);

#define MAXLINE 200

int
main(int argc, char *argv[])
{
  int sockfd, n;
  char recvline[MAXLINE + 1];
  struct sockaddr_in servaddr;

  if (argc != 2)
    err_quit("usage: a.out <ip address>");

  //  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  if ((sockfd = socket(999, SOCK_STREAM, 0)) < 0)
    err_quit("socket error");

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8585);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    err_quit("inet_pton error");

  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    err_quit("connect error");

  while ((n = read(sockfd, recvline, MAXLINE)) > 0)
    {
      recvline[n] = 0;
      if (fputs(recvline, stdout) == EOF)
        err_quit("fputs error");
    }

  close(sockfd);

  return EXIT_SUCCESS;
}
